﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors.Repository;
using eFatura;
using eFatura.Utility;
using System.Data;
using Extensions;

namespace ConfExpert.Forms.Araclar
{
    public partial class UserChanger : XPopupForm
    {
        DataTable dtUser;
        public UserChanger()
        {
            InitializeComponent();

            dtUser = AppSession.DataManager.ExecuteDataTable("GetKullanicilar");

            RepositoryItemGridLookUpEdit riCikisTipi = RepositoryManager.GetGLESablon(dtUser, "AdSoyad", "KullaniciId");
            riCikisTipi.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            gleUser.Properties.Assign(riCikisTipi);
            gleUser.EditValue = AppSession.UserInfo.UserId;
        }

        private void UserChanger_Load(object sender, EventArgs e)
        {
            ShowOnlyBar();
            gleUser.Closed += gleUser_Closed;
        }

        void gleUser_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            int userId = (int)gleUser.EditValue;
            DataRow newUser = dtUser.Select("KullaniciId=" + userId)[0];

            AppSession.UserInfo.UserId = newUser["KullaniciId"].ToIntZero();
            AppSession.UserInfo.DeptId = newUser["DepartmanId"].ToIntZero();
            AppSession.UserInfo.Email = newUser["ePosta"].ToString();
            AppSession.UserInfo.Name = newUser["AdSoyad"].ToString();
            AppSession.UserInfo.UserName = newUser["KullaniciAdi"].ToString();
            AppSession.MainForm.WhoAmI = newUser["KullaniciId"].ToIntZero();
            AppSession.MainForm.bsiUser.Caption = AppSession.UserInfo.UserName;
            Notify("Yeni Kullanıcı: " + newUser["AdSoyad"].ToString());
            this.DialogResult = DialogResult.OK;
        }
    }
}